package fr.enseirb.mycub.activity.component.layer;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

import android.app.Activity;
import android.database.Cursor;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.provider.BaseColumns;
import android.util.Log;
import fr.enseirb.mycub.PrefManager;
import fr.enseirb.mycub.R;
import fr.enseirb.mycub.activity.AbstractPreferencesActivity;
import fr.enseirb.mycub.activity.MainActivity;
import fr.enseirb.mycub.activity.component.LocationServiceComponent;
import fr.enseirb.mycub.activity.component.MapOverlayComponent;
import fr.enseirb.mycub.component.map.OverlayBundleItem;
import fr.enseirb.mycub.component.map.bike.BikeStationOverlay;
import fr.enseirb.mycub.component.map.bike.BikeStationsOverlay;
import fr.enseirb.mycub.db.BikeDBConstants;
import fr.enseirb.mycub.db.BikeDBHelper;
import fr.enseirb.mycub.db.DBAdapter;
import fr.enseirb.mycub.service.UpdateService;
import fr.enseirb.mycub.utils.SeekBarPreference;
import fr.enseirb.mycub.utils.Utils;

public class BikeLayer extends Layer{
	
	public BikeLayer(MapOverlayComponent moc){
	this(moc.getActivity(),moc.getLocationServiceComponent(), moc.getMapView(), moc.getMapOverlays());
	}
	
	public BikeLayer(MainActivity activity, LocationServiceComponent locationServiceComponent,
			MapView mMapView, List<Overlay> mMapOverlays){
		super(activity, locationServiceComponent,mMapView, mMapOverlays);
		
		//Log.i("MyCub", "activity : "+activity+" LocationServiceComponent : "+locationServiceComponent+" mapView : "+mMapView+" mMapsOverlay : "+mMapOverlays);
		BitmapDrawable greenmarker = (BitmapDrawable) activity.getResources().getDrawable(
				R.drawable.greenpin);
		BitmapDrawable redmarker = (BitmapDrawable) activity.getResources().getDrawable(
				R.drawable.redpin);
		BitmapDrawable orangemarker = (BitmapDrawable) activity.getResources().getDrawable(
				R.drawable.orangepin);

		greenmarker.setBounds(0, 0, greenmarker.getIntrinsicWidth(), greenmarker
				.getIntrinsicHeight());
		redmarker.setBounds(0, 0, redmarker.getIntrinsicWidth(), redmarker
				.getIntrinsicHeight());
		orangemarker.setBounds(0, 0, orangemarker.getIntrinsicWidth(), orangemarker
				.getIntrinsicHeight());
		mStationsOverlay = new BikeStationsOverlay(activity.getResources(), greenmarker, orangemarker, redmarker,
				mMapView, activity);
		mMapOverlays.add(mStationsOverlay);
		
	}

	class BikePopulateOverlays extends PopulateOverlays {

		public BikePopulateOverlays(Layer layer) {
			super(layer);
		}
		
		@Override
		protected int getOverlayCount() {
			return BikeDBHelper.getStationCount(mDBAdapter.getDB());
		}

		@Override
		protected ArrayList<Cursor> getOverlayCursor() {
			ArrayList<Cursor> cursor = new ArrayList<Cursor>();
			cursor.add(BikeDBHelper.getFilteredStationsCursor(
					mDBAdapter.getDB(),
					new String[] { BaseColumns._ID, BikeDBConstants.KEY_OPEN,
						BikeDBConstants.KEY_LATITUDE,
						BikeDBConstants.KEY_LONGITUDE,
						BikeDBConstants.KEY_BIKES,
						BikeDBConstants.KEY_SLOTS }, Utils
						.whereClauseFromFilter(PrefManager.mgr), null));
			return cursor;
		}

		@Override
		protected ArrayList<? extends OverlayBundleItem> getOverlayFromCursor(ArrayList<Cursor> cursor) {
			return mStationsOverlay.getOverlaysFromCursor(cursor,
					locationServiceComponent.isBound() ? locationServiceComponent.getMyLocation() : null,
							PrefManager.mgr.getBoolean(
									AbstractPreferencesActivity.ENABLE_DISTANCE_FILTER,
									false) ? PrefManager.mgr.getInt(
											AbstractPreferencesActivity.DISTANCE_FILTER,
											SeekBarPreference.DEFAULT_DISTANCE) : 0);
		}
	}

	class BikeUpdateOverlays extends UpdateOverlays {

		public BikeUpdateOverlays(Layer layer) {
			super(layer);
		}


		@Override
		protected int getOverlayCount() {
			return BikeDBHelper.getStationCount(mDBAdapter.getDB());
		}

		@Override
		protected Cursor getOverlayCursor() {
			return BikeDBHelper.getFilteredStationsCursor(
					mDBAdapter.getDB(),
					new String[] { BikeDBConstants.KEY_BIKES,
						BikeDBConstants.KEY_SLOTS,
						BikeDBConstants.KEY_OPEN }, Utils
						.whereClauseFromFilter(PrefManager.mgr),
						BaseColumns._ID);
		}

		@Override
		protected void updateOverlays(Cursor cursor, Iterator<? extends OverlayBundleItem> it) {
			BikeStationOverlay stationOverlay;
			while (it.hasNext() && !cursor.isLast() && !isCancelled()) {
				stationOverlay = (BikeStationOverlay)it.next();
				cursor.moveToNext();
				stationOverlay.setBikes(cursor.getInt(cursor
						.getColumnIndex(BikeDBConstants.KEY_BIKES)));
				stationOverlay.setSlots(cursor.getInt(cursor
						.getColumnIndex(BikeDBConstants.KEY_SLOTS)));
			}
		}
	}

	@Override
	public void createPopulateOverlaysTaskAndExecute() {
		mPopulateOverlays = (BikePopulateOverlays) new BikePopulateOverlays(this).execute();
	}

	@Override
	public void createUpdateOverlaysTaskAndExecute() {
		mUpdateOverlays = (UpdateOverlays) new BikeUpdateOverlays(this).execute();
	}

	@Override
	protected String getIntentAdd() {
		return UpdateService.ACTION_ADD_BIKE;
	}

	@Override
	protected String getIntentDelete() {
		return UpdateService.ACTION_DELETE_BIKE;
	}
	
	
}
